/**
 * Cobub Razor
 *
 * An open source analytics android sdk for mobile applications
 *
 * @package		Cobub Razor
 * @author		WBTECH Dev Team
 * @copyright	Copyright (c) 2011 - 2015, NanJing Western Bridge Co.,Ltd.
 * @license		http://www.cobub.com/products/cobub-razor/license
 * @link		http://www.cobub.com/products/cobub-razor/
 * @since		Version 0.1
 * @filesource
 */

package com.wbtech.ums;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;

public class NetworkUtil {
	private static final String TAG = "NetworkUtil";
	
	/*public static String Post(String url, String data) {

		CobubLog.d(TAG, "URL = " + url);
		CobubLog.d(TAG, "Data = " + data);

		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			StringEntity se = new StringEntity("content="
					+ URLEncoder.encode(data), HTTP.UTF_8);
			se.setContentType("application/x-www-form-urlencoded");
			httppost.setEntity(se);

			HttpResponse response = httpclient.execute(httppost);

			CobubLog.d(TAG, "Status code="
					+ response.getStatusLine().getStatusCode());

			String returnXML = EntityUtils.toString(response.getEntity());

			CobubLog.d(TAG, "returnString = " + URLDecoder.decode(returnXML));

			return URLDecoder.decode(returnXML);

		} catch (Exception e) {
			CobubLog.e(TAG, e);
			return null;
		}
	}*/
	
	public static String Post(String urlStr, String data){
		return Post(urlStr, data, "content=");
	}
	
	

	public static String Post(String urlStr, String data, String unEncodeKey) {
		HttpURLConnection connection = null;

		try {
			URL url = new URL(urlStr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setReadTimeout(5000);
			connection.setConnectTimeout(5000);
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setDoOutput(true);			
			connection.setUseCaches(false);			
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//			connection.setRequestProperty("accept", "*/*");
//			connection.setRequestProperty("connection", "Keep-Alive");
//			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");  
			 
			connection.connect();
			
			DataOutputStream outputStream  = new DataOutputStream(connection.getOutputStream());
			String entityData = unEncodeKey+URLEncoder.encode(data, "UTF-8");
//			String entityData = unEncodeKey+data;
//			System.out.println("json is :"+"json_str="+data);
//			System.out.println("entityData is :"+entityData);
			
			outputStream.writeBytes(entityData);
			outputStream.flush();
			outputStream.close();
			
			int responseCode = connection.getResponseCode();	
			
//			System.out.println("responseCode is :"+responseCode);
			if(HttpURLConnection.HTTP_OK == responseCode){
				CobubLog.d(TAG, "send success");
				
				StringBuilder builder = new StringBuilder();
				String line = new String();
				BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				while((line=reader.readLine())!=null){
					builder.append(line);
				}
				reader.close();
//				System.out.println("builder.toString is :"+builder.toString());
				return builder.toString();

			}
			return null;

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			if (null != connection) {
				connection.disconnect();
			}
		}

	}

	public static MyMessage parse(String str) {
		try {
			if (str == null)
				return null;
			JSONObject jsonObject = new JSONObject(str);
			MyMessage message = new MyMessage();
			message.setFlag(jsonObject.getInt("flag"));
			message.setMsg(jsonObject.getString("msg"));
			return message;
		} catch (JSONException e) {
			CobubLog.e(TAG, e);
			return null;
		} catch (Exception e1) {

			CobubLog.e(TAG, e1);
			return null;
		}
	}

}
